﻿#region 版权说明
/**********************************************************************
      * 功能说明:
      * 
      * CLR 版本:       4.0.30319.18033
      * 类 名 称:       StyleHelper
      * 命名空间:       Print
      * 创建时间:       2013-05-09 16:57:08
      * 作    者:       Devil月哥
      * 版    权:       StyleHelper说明：本代码版权归Devil月哥所有 All Rights Reserved (C) 2013 - 2013
      * 网    站:       http://www.xd5u.com
      * 邮    箱:       webmaster@xd5u.com
      * 唯一标识：      848344f4-ac14-472f-aae1-3b9e6fbf94e7  

      * 修改时间:
      * 修 改 人:
      * 
      ***********************************************************************/
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml.Linq;

namespace Prints
{
    public class StyleHelper
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public StyleHelper() { }

        #region 配置文件变量
        /// <summary>
        /// 配置文件路径
        /// </summary>
        public static string ConfigPath =
            System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "Reports/Reports.xml";

        #endregion

        #region 样式文件是否存在
        /// <summary>
        /// 样式文件是否存在(文件路径)
        /// </summary>
        /// <param name="path">文件路径</param>
        /// <returns></returns>
        public static bool IsFileExist(string path)
        {
            if (File.Exists(path))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        #endregion

        #region 添加
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="ConfigModel"></param>
        /// <returns></returns>
        public static bool AddStyle(Model.ModelPrintStyle ConfigModel)
        {
            bool returns = false;
            try
            {
                XElement rootNode = XElement.Load(ConfigPath);
                XElement newNode = new XElement("Express", new XAttribute("Id", ConfigModel.Id),
                    new XElement("StyleName", ConfigModel.StyleName),
                    new XElement("StyleFiles", ConfigModel.StyleFiles),
                    new XElement("StyleNote", ConfigModel.StyleNote)
                    );

                rootNode.Add(newNode);
                rootNode.Save(ConfigPath);
                returns = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return returns;
        }
        #endregion

        #region 修改
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="ConfigModel"></param>
        /// <returns></returns>
        public static bool UpdateStyle(Model.ModelPrintStyle ConfigModel)
        {
            bool returns = false;
            try
            {
                Checkremoteconfig();
                XElement rootNode = XElement.Load(ConfigPath);
                var targetNodes = from target in rootNode.Descendants("Express")
                                  where target.Attribute("Id").Value == ConfigModel.Id.ToString()
                                  select target;
                foreach (var node in targetNodes)
                {
                    node.Element("StyleName").SetValue(ConfigModel.StyleName);
                    node.Element("StyleFiles").SetValue(ConfigModel.StyleFiles);
                    node.Element("StyleNote").SetValue(ConfigModel.StyleNote);
                    returns = true;
                }
                rootNode.Save(ConfigPath);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return returns;
        }
        #endregion

        #region 删除
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public static bool DeleteStyle(Guid Id)
        {
            bool returns = false;
            try
            {
                XElement rootNode = XElement.Load(ConfigPath);
                var targetNodes = from target in rootNode.Descendants("Express")
                                  where target.Attribute("Id").Value.Equals(Id.ToString())
                                  select target;
                targetNodes.Remove();
                rootNode.Save(ConfigPath);
                returns = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return returns;
        }
        #endregion

        #region 是否存在
        /// <summary>
        /// 是否存在
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public static bool ExistsStyle(Guid Id)
        {
            bool returns = false;
            try
            {
                XElement rootNode = XElement.Load(ConfigPath);
                var targetNodes = from target in rootNode.Descendants("Express")
                                  where target.Attribute("Id").Value.Equals(Id.ToString())
                                  select target;
                if (targetNodes.Count<XElement>() > 0)
                {
                    returns = true;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return returns;
        }
        #endregion

        #region 根据Id获取
        /// <summary>
        /// 根据Id获取
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public static Model.ModelPrintStyle GetStyleById(Guid Id)
        {
            Model.ModelPrintStyle returns = new Model.ModelPrintStyle();
            try
            {
                XElement rootNode = XElement.Load(ConfigPath);
                var myTargetNodes = from myTarget in rootNode.Descendants("Express")
                                    where myTarget.Attribute("Id").Value.Equals(Id.ToString()) && myTarget.HasElements
                                    //select myTarget;
                                    select new Model.ModelPrintStyle
                                    {
                                        StyleName = myTarget.Element("StyleName") == null ?
                                        "" : myTarget.Attribute("StyleName").Value,

                                        StyleFiles = myTarget.Element("StyleFiles") == null ?
                                        "" : myTarget.Attribute("StyleFiles").Value,

                                        StyleNote = myTarget.Element("StyleNote") == null ?
                                        "" : myTarget.Attribute("StyleNote").Value
                                    };
                if (myTargetNodes.Count<Model.ModelPrintStyle>() == 1)
                {
                    foreach (var node in myTargetNodes)
                    {
                        returns = node;
                    }
                }
                return returns;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region 根据名称获取
        /// <summary>
        /// 根据名称获取
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public static Model.ModelPrintStyle GetStyleByStyleName(string Name)
        {
            Model.ModelPrintStyle returns = new Model.ModelPrintStyle();
            try
            {
                XElement rootNode = XElement.Load(ConfigPath);
                var myTargetNodes = from myTarget in rootNode.Descendants("Express")
                                    where myTarget.Attribute("StyleName").Value.Equals(Name) && myTarget.HasElements
                                    //select myTarget;
                                    select new Model.ModelPrintStyle
                                    {
                                        Id = myTarget.Element("Id") == null ?
                                        Guid.NewGuid() : new Guid( myTarget.Attribute("StyleName").Value),

                                        StyleName = myTarget.Element("StyleName") == null ?
                                        "" : myTarget.Attribute("StyleName").Value,

                                        StyleFiles = myTarget.Element("StyleFiles") == null ?
                                        "" : myTarget.Attribute("StyleFiles").Value,

                                        StyleNote = myTarget.Element("StyleNote") == null ?
                                        "" : myTarget.Attribute("StyleNote").Value
                                    };
                if (myTargetNodes.Count<Model.ModelPrintStyle>() == 1)
                {
                    foreach (var node in myTargetNodes)
                    {
                        returns = node;
                    }
                }
                return returns;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region 获取全部打印样式
        /// <summary>
        /// 获取全部打印样式
        /// </summary>
        /// <returns></returns>
        public static List<Model.ModelPrintStyle> GetAllStyle()
        {
            List<Model.ModelPrintStyle> returns = new List<Model.ModelPrintStyle>();
            try
            {
                XElement rootNode = XElement.Load(ConfigPath);
                var targetNodes = from target in rootNode.Descendants("Express")
                                  //select target;
                                  select new Model.ModelPrintStyle
                                  {
                                      Id = Guid.Parse(target.Attribute("Id").Value),
                                      StyleName = target.Element("StyleName").Value,
                                      StyleFiles = target.Element("StyleFiles").Value,
                                      StyleNote = target.Element("StyleNote").Value
                                  };
                foreach (var node in targetNodes)
                {
                    returns.Add(node);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return returns;
        }
        #endregion


        #region 检测配置
        /// <summary>
        /// 检查配置
        /// </summary>
        /// <returns></returns>
        public static bool Checkremoteconfig()
        {
            bool returns = false;
            try
            {
                if (IsFileExist(ConfigPath)) 
                { 
                    returns = true; 
                }
                else
                {
                    XElement rootNode = new XElement("Reports");
                    rootNode.Save(ConfigPath);
                    returns = true;
                }
                return returns;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

    }
}
